﻿using CheatMenu.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CheatMenu.Utils;

namespace CheatMenu.Menus
{
    public class MenuPrompt : MenuBase
    {
        public delegate void PromptButtonClickedDelegate(MenuPrompt sender, DialogResult pressedButton);

        public event PromptButtonClickedDelegate ButtonClicked;
        protected void OnButtonClicked(DialogResult pressedButton)
        {
            if (CloseOnButtonClicked)
                Shown = false;

            ButtonClicked?.Invoke(this, pressedButton);
        }

        private GUIStyle defaultFontLabelStyle;
        private GUIStyle defaultFontLabelHeaderStyle;
        private GUIStyle defaultFontControlWidthHeightButtonStyle;

        public string PromptHeader { get; set; }
        public string PromptText { get; set; }
        public readonly List<DialogResult> PromptButtons = new List<DialogResult>();
        public bool CloseOnButtonClicked { get; set; } = true;

        public MenuPrompt(string name, string text = null) : base(name, text)
        {
            Alignment = Alignment.Centre;
        }

        protected override Vector2 InitializeSize()
        => new Vector2((GUIManager.ControlWidth * 3) + (GUIManager.PaddingSize * 2), (GUIManager.ControlHeight * 2) + (GUIManager.PaddingSize * 2));

        protected override void InitializeGUI()
        {
            defaultFontLabelStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelColor
                }
            };

            defaultFontLabelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                fontStyle = FontStyle.Bold,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };

            defaultFontControlWidthHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedWidth = GUIManager.ControlWidth,
                fixedHeight = GUIManager.ControlHeight
            };
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    if (!string.IsNullOrEmpty(PromptHeader)) GUILayout.Label(PromptHeader, defaultFontLabelHeaderStyle);
                    if (!string.IsNullOrEmpty(PromptText)) GUILayout.Label(PromptText, defaultFontLabelStyle);
                }
                GUILayout.EndVertical();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        for (int i = 0; i < PromptButtons.Count; i++)
                        {
                            if(i > 0)
                                GUILayout.FlexibleSpace();

                            if (GUILayout.Button(Enum.GetName(typeof(DialogResult), PromptButtons[i]), defaultFontControlWidthHeightButtonStyle))
                                OnButtonClicked(PromptButtons[i]);
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();
        }
    }
}
